<?php

Yii::import('Business.models.Business');
class ApiController extends Controller
{
    // {{{ *** Members ***



    public $format = 'json';
    // }}}
    // {{{ filters
    /**
     * @return array action filters
     */
    public function filters()
    {
            return array();
    } // }}}
    // {{{ *** Actions ***
    // {{{ actionIndex
    public function actionIndex()
    {
        echo CJSON::encode(array(1, 2, 3));
    } // }}}
    // {{{ actionList
    public function actionList()
    {
        ////$this->_checkAuth();;
        switch($_GET['model'])
        {
            case 'business': // {{{
                $models = Business::model()->findAll();
                break; // }}}
            default: // {{{
                $this->sendResponse(501, sprintf('Error: Mode <b>list</b> is not implemented for model <b>%s</b>',$_GET['model']) );
                exit; // }}}
        }
        if(is_null($models)) {
            $this->sendResponse(200, sprintf('No items where found for model <b>%s</b>', $_GET['model']) );
        } else {
            $rows = array();
            foreach($models as $model)
                $rows[] = $model->attributes;

            $this->sendResponse(200, CJSON::encode($rows));
        }
    } // }}}
    // {{{ actionView
    /* Shows a single item
     *
     * @access public
     * @return void
     */
    public function actionView()
    {
        //$this->_checkAuth();;
        // Check if id was submitted via GET
        if(!isset($_GET['id']))
            $this->sendResponse(500, 'Error: Parameter <b>id</b> is missing' );

        switch($_GET['model'])
        {
            // Find respective model
            case 'business': // {{{
                $model = Business::model()->findByPk($_GET['id']);
                break; // }}}
            default: // {{{
                $this->sendResponse(501, sprintf('Mode <b>view</b> is not implemented for model <b>%s</b>',$_GET['model']) );
                exit; // }}}
        }
        if(is_null($model)) {
            $this->sendResponse(404, 'No Item found with id '.$_GET['id']);
        } else {
            $this->sendResponse(200, $this->_getObjectEncoded($_GET['model'], $model->attributes));
        }
    } // }}}
    // {{{ actionCreate
    /**
     * Creates a new item
     *
     * @access public
     * @return void
     */
    public function actionCreate()
    {
        //$this->_checkAuth();;

        switch($_GET['model'])
        {
            // Get an instance of the respective model
            case 'business': // {{{
                $model = new Business;
                break; // }}}
            default: // {{{
                $this->sendResponse(501, sprintf('Mode <b>create</b> is not implemented for model <b>%s</b>',$_GET['model']) );
                exit; // }}}
        }
        // Try to assign POST values to attributes
        foreach($_POST as $var=>$value) {
            // Does the model have this attribute?
            if($model->hasAttribute($var)) {
                $model->$var = $value;
            } else {
                // No, raise an error
                $this->sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']) );
            }
        }
        // Try to save the model
        if($model->save()) {
            // Saving was OK
            $this->sendResponse(200, $this->_getObjectEncoded($_GET['model'], $model->attributes) );
        } else {
            // Errors occurred
            $msg = "<h1>Error</h1>";
            $msg .= sprintf("Couldn't create model <b>%s</b>", $_GET['model']);
            $msg .= "<ul>";
            foreach($model->errors as $attribute=>$attr_errors) {
                $msg .= "<li>Attribute: $attribute</li>";
                $msg .= "<ul>";
                foreach($attr_errors as $attr_error) {
                    $msg .= "<li>$attr_error</li>";
                }
                $msg .= "</ul>";
            }
            $msg .= "</ul>";
            $this->sendResponse(500, $msg );
        }

        var_dump($_REQUEST);
    } // }}}
    // {{{ actionUpdate
    /**
     * Update a single iten
     *
     * @access public
     * @return void
     */
    public function actionUpdate()
    {
        ////$this->_checkAuth();;

        // Get PUT parameters
        parse_str(file_get_contents('php://input'), $put_vars);

        switch($_GET['model'])
        {
            // Find respective model
            case 'business': // {{{
                $model = Business::model()->findByPk($_GET['id']);
                break; // }}}
            default: // {{{
                $this->sendResponse(501, sprintf('Error: Mode <b>update</b> is not implemented for model <b>%s</b>',$_GET['model']) );
                exit; // }}}
        }
        if(is_null($model))
            $this->sendResponse(400, sprintf("Error: Didn't find any model <b>%s</b> with ID <b>%s</b>.",$_GET['model'], $_GET['id']) );

        // Try to assign PUT parameters to attributes
        foreach($put_vars as $var=>$value) {
            // Does model have this attribute?
            if($model->hasAttribute($var)) {
                $model->$var = $value;
            } else {
                // No, raise error
                $this->sendResponse(500, sprintf('Parameter <b>%s</b> is not allowed for model <b>%s</b>', $var, $_GET['model']) );
            }
        }
        // Try to save the model
        if($model->save()) {
            $this->sendResponse(200, sprintf('The model <b>%s</b> with id <b>%s</b> has been updated.', $_GET['model'], $_GET['id']) );
        } else {
            $msg = "<h1>Error</h1>";
            $msg .= sprintf("Couldn't update model <b>%s</b>", $_GET['model']);
            $msg .= "<ul>";
            foreach($model->errors as $attribute=>$attr_errors) {
                $msg .= "<li>Attribute: $attribute</li>";
                $msg .= "<ul>";
                foreach($attr_errors as $attr_error) {
                    $msg .= "<li>$attr_error</li>";
                }
                $msg .= "</ul>";
            }
            $msg .= "</ul>";
            $this->sendResponse(500, $msg );
        }
    } // }}}
    // {{{ actionDelete
    /**
     * Deletes a single item
     *
     * @access public
     * @return void
     */
    public function actionDelete()
    {
        //$this->_checkAuth();;

        switch($_GET['model'])
        {
            // Load the respective model
            case 'business': // {{{
                $model = Business::model()->findByPk($_GET['id']);
                break; // }}}
            default: // {{{
                $this->sendResponse(501, sprintf('Error: Mode <b>delete</b> is not implemented for model <b>%s</b>',$_GET['model']) );
                exit; // }}}
        }
        // Was a model found?
        if(is_null($model)) {
            // No, raise an error
            $this->sendResponse(400, sprintf("Error: Didn't find any model <b>%s</b> with ID <b>%s</b>.",$_GET['model'], $_GET['id']) );
        }

        // Delete the model
        $num = $model->delete();
        if($num>0)
            $this->sendResponse(200, sprintf("Model <b>%s</b> with ID <b>%s</b> has been deleted.",$_GET['model'], $_GET['id']) );
        else
            $this->sendResponse(500, sprintf("Error: Couldn't delete model <b>%s</b> with ID <b>%s</b>.",$_GET['model'], $_GET['id']) );
    } // }}}
    // }}} End Actions
    // {{{ Other Methods
    // {{{ getStatusCodeMessage
    // {{{ _checkAuth


}

/* vim:set ai sw=4 sts=4 et fdm=marker fdc=4: */
